package org.greenrobot.eventbus.harmony.slice;

import ohos.agp.components.Component;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import org.greenrobot.eventbus.harmony.model.IOThreadEvent;
import org.greenrobot.eventbus.harmony.model.MainThreadEvent;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        //发送事件，会主线程中执行
        findComponentById(ResourceTable.Id_send_event_to_main_thread_btn).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                EventBus.getDefault().post(new MainThreadEvent());
            }
        });
        //发送事件，会在子线程中执行
        findComponentById(ResourceTable.Id_send_event_to_io_thread_btn).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                EventBus.getDefault().post(new IOThreadEvent());
            }
        });
    }
}
